import { Controller, Get, Post, Put, Delete, Body, Param, Query, BadRequestException } from '@nestjs/common';
import { VideoGroupService } from './video-group.service';
import { VideoGroup } from './video-group.entity';
import { PaginationResponse } from '@/common/pagination-response.dto';

@Controller('video-groups')
export class VideoGroupController {
    constructor(private readonly videoGroupService: VideoGroupService) { }

    // 创建视频组
    @Post()

    async create(@Body() videoGroup: VideoGroup): Promise<VideoGroup> {
        return this.videoGroupService.create(videoGroup);
    }

    // 根据 ID 查询视频组
    @Get(':id')

    async findOne(@Param('id') id: number): Promise<VideoGroup> {
        return this.videoGroupService.findOne(id);
    }

    // 模糊查询视频组
    @Get()

    async findByGroupName(@Query('groupName') groupName: string): Promise<VideoGroup[]> {
        return this.videoGroupService.findByGroupName(groupName);
    }

    // 获取所有视频组
    @Get()

    async findAll(): Promise<VideoGroup[]> {
        return this.videoGroupService.findAll();
    }

    // 更新视频组
    @Put(':id')

    async update(@Param('id') id: number, @Body() videoGroup: VideoGroup): Promise<VideoGroup> {
        return this.videoGroupService.update(id, videoGroup);
    }

    // 批量删除视频组的 DELETE 接口
    @Delete()

    async removeMultiple(@Body() body: { ids: number[] }): Promise<void> {
        // console.log('接收到的请求体:', body); // 添加调试日志
        if (!body || !Array.isArray(body.ids)) {
            throw new BadRequestException('Invalid request body. Expected an object with an "ids" array.');
        }
        const { ids } = body;
        return this.videoGroupService.removeMultiple(ids);
    }

    // 分页和模糊查询视频组的 POST 接口
    @Post('search')

    async findAllWithPaginationAndSearch(@Body() body: { pageSize: number; currentPage: number; groupName?: string }) {
        const { pageSize, currentPage, groupName } = body;
        const { data, total } = await this.videoGroupService.findAllWithPaginationAndSearch(
            pageSize,
            currentPage,
            groupName,
        );

        return new PaginationResponse(
            pageSize,
            currentPage,
            total,
            data,
        );
    }
}